package com.wolfking.jeesite.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 角色和菜单的关联关系
 *
 * @author wolfking@赵伟伟
 * @mail zww199009@163.com
 * @创作日期 2017年5月13日 下午9:19:36
 * @copyright wolfking
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("sys_role_menu")
public class RoleMenuRelation {

    @TableId(value = "id", type = IdType.AUTO)
    protected Integer id;
    @TableField("role_id")
    private Integer roleId;
    @TableField("menu_id")
    private Integer menuId;

    public static List<RoleMenuRelation> assemblyRelation(Role role) {
        Integer roleId = role.getId();
        String menuIds = role.getMenuIds();
        List<RoleMenuRelation> list = Lists.newArrayList();
        if (StringUtils.isNotEmpty(role.getMenuIds()))
            for (String menuId : menuIds.split(","))
                if (StringUtils.isNotEmpty(menuId))
                    list.add(new RoleMenuRelation(null,roleId, Integer.parseInt(menuId)));
        return list;
    }
}
